package com.dotools.toutiaolibrary;

import android.content.Context;
import android.graphics.Point;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.MainThread;
import androidx.annotation.RequiresApi;

import com.bykv.vk.openvk.ISplashClickEyeListener;
import com.bykv.vk.openvk.TTSphObject;
import com.bykv.vk.openvk.TTVfConstant;
import com.bykv.vk.openvk.TTVfManager;
import com.bykv.vk.openvk.TTVfNative;
import com.bykv.vk.openvk.VfSlot;
import com.dotools.toutiaolibrary.util.SplashClickEyeManager;
import com.dotools.toutiaolibrary.util.UIUtils;


import api.txSplash.Splash_API_TT;

public class TT_Splash extends Splash_API_TT {
    private SplashClickEyeManager mSplashClickEyeManager;
    private TTVfNative mTTAdNative;
    //开屏广告加载超时时间,建议大于1000,这里为了冷启动第一次加载到广告并且展示,示例设置了5000ms
    private final int AD_TIME_OUT = 5000;


    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    public void LoadSplash(Context context, String appid, String adposid, int showMode, int downloadMode, boolean isClickEye,int clickEyePosition, SplashListener listener) {
        if (appid != null) {
            if (appid.trim().equals("")) {
                listener.onError(-2, "appid is null");
                return;
            }
        } else {
            return;
        }
        WindowManager window = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        Point point =new Point();
        window.getDefaultDisplay().getRealSize(point);
        TTVfManager manager = TTManagerHolder.get();
        if (manager != null) {
            mTTAdNative = manager.createVfNative(context.getApplicationContext());
                //开屏广告参数
            VfSlot  adSlot = new VfSlot.Builder()
                        .setCodeId(adposid)
                        .setImageAcceptedSize(point.x, point.y)
                        .setExpressViewAcceptedSize(UIUtils.px2dip(context,point.x),UIUtils.px2dip(context,point.y))
                        .build();
            if (showMode == 1) {
                adSlot.setSplashButtonType(TTVfConstant.SPLASH_BUTTON_TYPE_DOWNLOAD_BAR);
            }
            if (downloadMode == 1) {
                adSlot.setDownloadType(TTVfConstant.DOWNLOAD_TYPE_POPUP);
            }
            //调用开屏广告异步请求接口
            mTTAdNative.loadSphVs(adSlot, new TTVfNative.SphVfListener() {
                @Override
                @MainThread
                public void onError(int code, String message) {
                    listener.onError(code, message);
                }

                @Override
                @MainThread
                public void onTimeout() {
                    listener.onTimeout();
                }

                @Override
                public void onSphVsLoad(TTSphObject ttSphObject) {
                    if (ttSphObject == null) {
                        listener.onError(-1, "TTSplashAd NULL");
                        return;
                    }
                    View vie = ttSphObject.getSplashView();
                    //初始化开屏点睛相关数据
                    ttSphObject.setSplashClickEyeListener(new ISplashClickEyeListener() {
                        @Override
                        public void onSplashClickEyeAnimationStart() {

                        }

                        @Override
                        public void onSplashClickEyeAnimationFinish() {

                        }

                        @Override
                        public boolean isSupportSplashClickEye(boolean isSupport) {
                            Log.e("isSupportSplashClickEye", isSupport + "");
                            if(isClickEye){
                                mSplashClickEyeManager.setSupportSplashClickEye(true);
                            }else{
                                mSplashClickEyeManager.setSupportSplashClickEye(isSupport);
                            }
                            return false;
                        }
                    });
                    mSplashClickEyeManager = SplashClickEyeManager.getInstance();
                    //设置SplashView的交互监听器
                    ttSphObject.setSplashInteractionListener(new TTSphObject.VfInteractionListener() {

                        @Override
                        public void onClicked(View view, int type) {
                            listener.onClicked(view, type);
                            mSplashClickEyeManager.setSupportSplashClickEye(false);//如果onClicked后 设置成false  其他页面根据此判断就不会展示点睛了
                            mSplashClickEyeManager.clearSplashStaticData();
                        }

                        @Override
                        public void onShow(View view, int type) {
                            listener.onShow(view, type);
                        }

                        @Override
                        public void onSkip() {
                            listener.onSkip();
                        }

                        @Override
                        public void onTimeOver() {
                            listener.onTimeOver();
                        }
                    });
                    mSplashClickEyeManager.setSplashInfo(context, ttSphObject, vie,clickEyePosition);
                    //获取SplashView
                    listener.onLoaded(vie);
                }
            }, AD_TIME_OUT);
        }else {
            listener.onError(666, "TTVfManager is null  可能原因sdk未初始化成功");
        }
    }


}
